<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');

class Games extends Controller 
{
	const DEFAULT_CATEGORY = 'console';
	/*
	*	construct
	*	@PARA: null
	*	@RETURN: null
	*/
	public function __construct()
	{
		parent::Controller();
	}
	
	/*
	*	index - default function
	*	@PARA: $consoleID
	*	@RETURN: null
	*/	
	public function index()
	{
	}

	/*
	*	listGames
	*	@PARA: $consoleID , $genreID
	*	@RETURN: null
	*/	
	public function listGames($category, $categoryID)
	{	
		$data['menuView'] = $this->_getMenuView($category);
		$start = $this->uri->segment(5,0);		
		$limitPerPage = 9;
		
		//get production	
		$productionData['category'] = $category;
		
		$this->load->model('games_model', 'gamesModel');				
		$productionData['gamesList'] = $this->gamesModel->getGamesList($category, $categoryID, $start, $limitPerPage);

		$this->load->library('pagination');
		$this->load->helper('url');		
		$config['base_url']     = site_url('games/listGames/'.$category.'/'.$categoryID);   
		$config['total_rows']   = $this->gamesModel->categoryRecordCount;
		$config['per_page']     = $limitPerPage;
		$config['uri_segment']     = 5;
		
		$this->pagination->initialize($config);

		$productionData['pageLinks'] = $this->pagination->create_links();

		$data['productionView'] = $this->load->view('games/list_games', $productionData, true);
		
		$this->load->library('layout');
		$this->layout->render_page('homepage/main_page', $data);
	}
	
	/*
	*	viewGame
	*	@PARA: gameID
	*	@RETURN: null
	*/	
	public function viewGame($gameID)
	{
		$data['menuView'] = $this->_getMenuView();
		
		//production list
	
		$this->load->model('games_model', 'gamesModel');
		$productionData['gameDetails'] = $this->gamesModel->retrieve_by_pkey($gameID);		
		
		$this->load->model('reviews_model', 'reviewsModel');
		$productionData['reviewDetails'] = $this->reviewsModel->getLatestReview($gameID);

		$data['productionView'] = $this->load->view('games/view_game', $productionData, true);

		$this->load->library('layout');			
		$this->layout->render_page('homepage/main_page', $data);							
	}
	
	/*
	*	addGame
	*	@PARA: null
	*	@RETURN: null
	*/	
	public function addGame()
	{
		$this->load->helper('url');
		$submit = $this->input->post('submit');

		if(!$submit)
		{
			$data['menuView'] = $this->_getMenuView();
			
			$this->load->model('consoles_model', 'consolesModel');
			$prudctionData['consolesList'] = $this->consolesModel->retrieveOptions();

			$this->load->model('genres_model', 'genresModel');
			$prudctionData['genresList'] = $this->genresModel->retrieveOptions();

			$data['productionView'] = $this->load->view('games/add_game', $prudctionData, true);					

			$this->load->library('layout');
			$this->layout->render_page('homepage/main_page', $data);						

		}
		else
		{
			$data = $this->_getFormValues();
			$this->load->model('games_model', 'gamesModel');
			$newID = $this->gamesModel->add($data);
			redirect('/games/viewGame/'.$newID, 'location');		
		}
	}
	
	/*
	*	getFormValues
	*	@PARA: null
	*	@RETURN: $data
	*	@ACCESS: private
	*/
	private function _getFormValues()
	{
		$data['id']			= $this->input->post('id', TRUE);
		$data['name']		= $this->input->post('name', TRUE);
		$data['console_id']	= $this->input->post('console_id', TRUE);		
		$data['genre_id']	= $this->input->post('genre_id', TRUE);		

		$data['publisher']		= $this->input->post('publisher', TRUE);		
		$data['released_date']	= $this->input->post('released_date', TRUE);		
		$data['language']		= $this->input->post('language', TRUE);		
		$data['player_number']	= $this->input->post('player_number', TRUE);		
		$data['format']			= $this->input->post('format', TRUE);		

		return $data;
	}
	
	/*
	*	getMenuView - this is duplcated in games. Have to think about how to resolve that.
	*	@PARA: catgory
	*	@RETURN: $menuView
	*	@ACCESS: private
	*/
	private function _getMenuView($category=null)
	{
		$menuView= '';
		$this->load->model('menus_model', 'menusModel');
		$menuData = $this->menusModel->getMenuData();
		if($menuData)
		{
			$menuView = $this->load->view('homepage/category_menus', $menuData, true);
		}
		return $menuView;
	}
}
?>